<?php
class StructureController extends Controller{
	private $modules = array();
	private $themes = array();
	public function actionIndex()
	{
		Yii::import('application.modules.default.models.Menu');
		$model = new Menu;
		$dataProvider=new CActiveDataProvider('Menu');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionModule(){
		//echo Yii::app()->modulePath;
		//$modulesListRaw = scandir(Yii::app()->modulePath, 1);
		$modulesDir  = opendir(Yii::app()->modulePath.DIRECTORY_SEPARATOR);
		$system = new System;
		if(isset($_POST['selected_modules']))
		{
			//die(print_r($_POST['selected_modules']));
			foreach($_POST['selected_modules'] as $index=>$modKey){
				$modulePath = Yii::app()->modulePath.DIRECTORY_SEPARATOR.$modKey;
				if(is_dir($modulePath) && file_exists($modulePath.DIRECTORY_SEPARATOR.ucfirst($modKey).'Module.php')){
					Yii::import('application.modules.'.$modKey.'.*');
					$moduleVars = get_class_vars(ucfirst($modKey).'Module');
					$moduleVars['info']['id']=$modKey;
					if(!$system->exists('name=:name AND type=:type',array(':name'=>$modKey,':type'=>'module'))){
						$system->attributes = array(
							'name'=>$modKey,
							'type'=>'module',
							'status'=>1,
							'weight'=>0,
							'info'=>json_encode($moduleVars['info']),
						);
						$system->save(false);
					}else{
						$currentMod = $system->findByPk(array('name'=>$modKey,'type'=>'module'));
						$currentMod->attributes = array(
							'name'=>$modKey,
							'type'=>'module',
							'status'=>1,
							'weight'=>0,
							'info'=>json_encode($moduleVars['info']),
						);
						$currentMod->update();
					}
					if(method_exists(ucfirst($modKey).'Module', 'install')){
						call_user_func(array(ucfirst($modKey).'Module', 'install'));
					}
				}			
			}
		}
		//$enabledModules = $system->findAll('status=:status',array(':status'=>1));
		//print_r($enabledModules);
		while (false !== ($filename = readdir($modulesDir))) {
			$itemPath = Yii::app()->modulePath.DIRECTORY_SEPARATOR.$filename;
			if(is_dir($itemPath) && file_exists($itemPath.DIRECTORY_SEPARATOR.ucfirst($filename).'Module.php')){
				
				Yii::import('application.modules.'.$filename.'.*');
				$module = ucfirst($filename).'Module';
				$moduleVars = get_class_vars($module);
				//print_r($moduleVars);
				if(isset($moduleVars['info']) && is_array($moduleVars['info'])){
					$moduleVars['info']['id']=$filename;
					$currentMod = $system->exists('name=:name AND status=:status AND type=:type',array(':name'=>$filename,':status'=>1,':type'=>'module'));
					if($currentMod){
						$moduleVars['info']['status'] = 1;
					}else{
						$moduleVars['info']['status'] = 0;
					}
					$this->modules[] = $moduleVars['info'];
				}
				
			}
		}
		//print_r($this->modules);
		//die();
		$dataProvider=new CArrayDataProvider($this->modules, array(
		    'id'=>'modules',
		    'sort'=>array(
		        'attributes'=>array(
		             'id', 'name', 'version',
		        ),
		    ),
		    'pagination'=>array(
		        'pageSize'=>10,
		    ),
		));
		//die(print_r($dataProvider));
		$this->render('module',array('dataProvider'=>$dataProvider,'model'=>$system));
	}
	public function actionTheme(){
		$themesDir  = opendir(Yii::app()->basePath.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR);
		
		$system = new System;
		if(isset($_POST['selected_themes']))
		{
			//die(print_r($_POST['selected_modules']));
			foreach($_POST['selected_themes'] as $index=>$themeKey){
				$themePath = Yii::app()->basePath.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR.$themeKey;
				if(is_dir($themePath) && file_exists($themePath.DIRECTORY_SEPARATOR.ucfirst($themeKey).'Theme.php')){
					require_once $themePath.DIRECTORY_SEPARATOR.ucfirst($themeKey).'Theme.php';
					$themeVars = get_class_vars(ucfirst($themeKey).'Theme');
					$themeVars['id']=$themeKey;
					$themeVars["regions"]=array("header","left","content","right","bottom");
					if(!$system->exists('name=:name AND type=:type',array(':name'=>$themeKey,':type'=>'theme'))){
						$system->attributes = array(
							'name'=>$themeKey,
							'type'=>'theme',
							'status'=>1,
							'weight'=>0,
							'info'=>json_encode($themeVars),
						);
						$system->save(false);
					}else{
						$currentTheme = $system->findByPk(array('name'=>$themeKey,'type'=>'theme'));
						$currentTheme->attributes = array(
							'name'=>$themeKey,
							'type'=>'theme',
							'status'=>1,
							'weight'=>0,
							'info'=>json_encode($themeVars),
						);
						$currentTheme->update();
					}
					if(method_exists(ucfirst($themeKey).'Theme', 'install')){
						call_user_func(array(ucfirst($themeKey).'Theme', 'install'));
					}
				}			
			}
		}
		
		while (false !== ($filename = readdir($themesDir))) {
			$itemPath = Yii::app()->basePath.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR.$filename;
			if(is_dir($itemPath) && file_exists($itemPath.DIRECTORY_SEPARATOR.ucfirst($filename).'Theme.php')){
				//Yii::setPathOfAlias('application.themes',Yii::app()->basePath.DIRECTORY_SEPARATOR.'themes');				
				//Yii::import('application.themes.'.$filename.'*');
				require_once $itemPath.DIRECTORY_SEPARATOR.ucfirst($filename).'Theme.php';
				$theme = ucfirst($filename).'Theme';
				$themeVars = get_class_vars($theme);
				//print_r($themeVars);
				$themeVars['id'] = $filename;
				//print_r($moduleVars);
				if(isset($themeVars['name']) && !empty($themeVars['name'])){
					$currentTheme = $system->exists('name=:name AND type=:type AND status=:status',array(':name'=>$filename,':status'=>1,':type'=>'theme'));
					if($currentTheme){
						$themeVars['status'] = 1;
					}else{
						$themeVars['status'] = 0;
					}
					$this->themes[] = $themeVars;
				}
				
			}
		}

		$dataProvider=new CArrayDataProvider($this->themes, array(
		    'id'=>'themes',
		    'sort'=>array(
		        'attributes'=>array(
		             'id', 'name', 'version',
		        ),
		    ),
		    'pagination'=>array(
		        'pageSize'=>10,
		    ),
		));
		
		$this->render('theme',array('dataProvider'=>$dataProvider));
	}
	/**
	 * This action is for menu listing
	 */
	public function actionMenu(){
		Yii::import('application.modules.default.models.Menu');
		$model = new Menu;
		$dataProvider=new CActiveDataProvider('Menu');
		$this->render('menu',array(
			'dataProvider'=>$dataProvider,
		));
	}
	/**
	 * This action is for create new menu
	 */
	public function actionCreateMenu(){
		Yii::import('application.modules.default.models.Menu');
		$model = new Menu;
		
		if(isset($_POST['Menu']))
		{
			$model->attributes = $_POST['Menu'];
			if($model->validate()){
				$model->save(false);
				$this->redirect(array('menulink','name'=>$model->menu_name));
			}
		}
		
		$this->render('createmenu',array('model'=>$model));
	}
	/**
	 * Add new menu links into menu
	 * Enter description here ...
	 */
	public function actionMenuLink(){
		$this->render('menulink');
	}
	public function actionDeleteMenu(){
		Yii::import('application.modules.default.models.Menu');
		Yii::import('application.modules.default.models.MenuLink');		
		$menu = new Menu;
		$menulink = new MenuLink;
		
		$transaction=Yii::app()->db->beginTransaction();
		try{
			$menu->deleteByPk(array('menu_name'=>$_GET['name'],'title'=>$_GET['title']));
			$menulink->deleteAll('menu_name=:menu_name',array(':menu_name'=>$_GET['name']));			
			$transaction->commit();
		}catch (Exception $e){
			$transaction->rollBack();					
		}
	}
	/**
	 * This action is for create new menu
	 */
	public function actionEditMenu(){
		Yii::import('application.modules.default.models.Menu');
		$model = new Menu;
		$menu = $model->findByPk(array('menu_name'=>$_GET['name'],'title'=>$_GET['title']));			
		$this->render('createmenu',array('model'=>$menu));
	}
	/** 
	 * Enter description here ...
	 */
	public function actionCreateMenuLink(){
		Yii::import('application.modules.default.models.MenuLink');
		Yii::import('application.modules.default.models.Menu');
		$model = new MenuLink;
		$menu = Menu::model()->find('menu_name=:menu_name AND title=:title',array(":menu_name"=>$_GET['name'],":title"=>$_GET['title']));
		$model->menu_name = $menu->menu_name;
		if(isset($_POST['MenuLink']))
		{
			$model->attributes = $_POST['MenuLink'];
			//die(print_r($_POST));
			if($model->validate()){
				$model->save(false);
				$this->redirect(array('menulink','name'=>$menu->menu_name));
			}
		}
		
		$this->render('createmenulink',array('model'=>$model,'menu'=>$menu));
	}
	/**
	 * This function is for block display
	 */
	public function actionBlock(){
		Yii::import('application.modules.default.models.Block');
		$system = new System;
		$themes = $system->findAll('type=:type AND status=:status',array(":type"=>"theme",":status"=>1));
		//print_r($themes);
		//die();
		$this->render('block',array('themes'=>$themes,'model'=>new Block));
	}
}
